package com.lucien.admin.controller.system;

import com.lucien.common.dto.UserList;
import com.lucien.common.fornt.SysRelationFront;
import com.lucien.common.result.GlobalResult;
import com.lucien.system.service.SysRelationShipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Lucien
 * @Date 2021/5/23 14:10
 * @Version 1.0
 * @Description:
 */
@RestController
@RequestMapping("/ship")
@Api(tags = "好友接口")
public class SysRelationController {

    @Autowired
    private SysRelationShipService relationShipService;

    @PostMapping("/append")
    @ApiOperation(value = "添加好友")
    public GlobalResult appendFriend(@RequestParam(name = "username", required = true) String username){
        int result = relationShipService.appendFriend(username);
        if (result == 1){
            return GlobalResult.success("添加成功", result);
        }else {
            return GlobalResult.error("添加失败！！！");
        }
    }

    @GetMapping("/userlist")
    @ApiOperation(value = "获取好友列表")
    public GlobalResult selectUserList(){
        List<UserList> result = relationShipService.userList();
        if (result.size() != 0){
            return GlobalResult.success("获取成功", result);
        }else {
            return GlobalResult.success("您还没有添加任何好友", result);
        }
    }

    @GetMapping("/{userId}")
    @ApiOperation(value = "删除好友")
    public GlobalResult delFriend(@PathVariable("userId") Integer userId){
        int result = relationShipService.delFriend(userId);
        if (result == 1){
            return GlobalResult.success("删除成功", result);
        }else {
            return GlobalResult.error("删除失败");
        }
    }

    @GetMapping("/isfriend/{userId}/{friendId}")
    @ApiOperation(value = "判断是否为好友关系")
    public GlobalResult isFriend(@PathVariable("userId") Integer userId, @PathVariable("friendId") Integer friendId){
        SysRelationFront result = relationShipService.isFriends(userId, friendId);
        return GlobalResult.success(result);
    }

    @GetMapping("/isfriends/{userId}/{friendId}")
    @ApiOperation(value = "判断是否为好友关系")
    public GlobalResult isFriends(@PathVariable("userId") Integer userId, @PathVariable("friendId") Integer friendId){
        boolean result = relationShipService.isFriend(userId, friendId);
        return GlobalResult.success(result);
    }
}
